package net.qhzw.common.util;

import com.github.pagehelper.util.StringUtil;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;

/***
 * 读取txt文件工具类
 */
public class TxtUtil {


    /***
     * 按行读取txt文件并返回一个List<String>集合
     * @return
     */
    public static List<String> getFileContent(File file) {
        List<String> strList = new ArrayList<>();
        //File file = new File(path);
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            // 将字节流向字符流转换
            inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8");
            // 创建字符流缓冲区
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            // 按行读取
            while ((line = bufferedReader.readLine()) != null) {
                strList.add(line);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return strList;
    }

    /***
     * 转换 为map
     * @param list
     * @return
     */
    public static Map<String, String> getFileContent(List<String> list) {
        List<String> result = new ArrayList<>();
        if (list == null || list.isEmpty()) {
            return null;
        }
        for (String s : list) {
            if (s.contains("UL CORNER LONGITUDE") || s.contains("UL CORNER LATITUDE")
                    || s.contains("UR CORNER LONGITUDE") || s.contains("UR CORNER LATITUDE")
                    || s.contains("LR CORNER LONGITUDE") || s.contains("LR CORNER LATITUDE")
                    || s.contains("LL CORNER LONGITUDE") || s.contains("LL CORNER LATITUDE")) {
                result.add(s);
            }
        }

        Map<String, String> resultMap = new HashMap<>();
        for (String s : result) {
            String[] split = s.replaceAll(" ", "").split("=");
            resultMap.put(split[0], split[1]);
        }

        Map<String, String> resultMap2 = new HashMap<>();
        for (Map.Entry<String, String> entry : resultMap.entrySet()) {
            String value = entry.getValue();
            String key = entry.getKey();
            if (value.endsWith("N")) {
                String n = value.replaceAll("N", "");
                double v = latAndLonToDecimal(n);
                resultMap2.put(key, String.valueOf(v));
            } else if (value.endsWith("S")) {
                String s1 = value.replaceAll("S", "");
                String tem = "-" + s1;
                double v = latAndLonToDecimal(tem);
                resultMap2.put(key, String.valueOf(v));
            } else if (value.endsWith("W")) {
                String s1 = value.replaceAll("W", "");
                String tem = "-" + s1;
                double v = latAndLonToDecimal(tem);
                resultMap2.put(key, String.valueOf(v));
            } else if (value.endsWith("E")) {
                String e = value.replaceAll("E", "");
                double v = latAndLonToDecimal(e);
                resultMap2.put(key, String.valueOf(v));
            }
        }

        return resultMap2;
    }


    /***
     * 经纬度转小数   latAndLonToDecimal
     * @param latlng
     * @return
     */
    private static double latAndLonToDecimal(String latlng) {
        double du = Double.parseDouble(latlng.substring(0, latlng.indexOf("°")));
        double fen = Double.parseDouble(latlng.substring(latlng.indexOf("°") + 1, latlng.indexOf("\'")));
        double miao = Double.parseDouble(latlng.substring(latlng.indexOf("\'") + 1, latlng.indexOf("\"")));
        if (du < 0)
            return -(Math.abs(du) + (fen + (miao / 60)) / 60);
        return du + (fen + (miao / 60)) / 60;
    }


}
